﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Tools
{
    public partial class IncrementRenameForm : UserControl
    {
        public IncrementRenameForm()
        {
            InitializeComponent();
            Dictionary<String, String> keys = new Dictionary<String, String>();
            FileStream fileStream = new FileStream("incrementRename.data", FileMode.OpenOrCreate);
            if (fileStream.Length > 0)
            {
                BinaryFormatter binaryFormatter = new BinaryFormatter();

                keys = binaryFormatter.Deserialize(fileStream) as Dictionary<String, String>;

                //string pdfPathTextBoxText = "";
                //keys.TryGetValue("pdfPathTextBoxText", out pdfPathTextBoxText);
                //pdfPathTextBox.Text = pdfPathTextBoxText;

                string sourceFileNameText = "";
                keys.TryGetValue("sourceFileNameText", out sourceFileNameText);
                sourceFileNameTextBox.Text = sourceFileNameText;

                string sourceFilePathText = "";
                keys.TryGetValue("sourceFilePathText", out sourceFilePathText);
                sourceFilePathTextBox.Text = sourceFilePathText;

                string spitText = "";
                keys.TryGetValue("spitText", out spitText);
                spitTextBox.Text = spitText;
            }
            fileStream.Close();
        }

        private void submit_Click(object sender, EventArgs e)
        {
            Console.WriteLine("sourceFileNameTextBox.Text:" +sourceFileNameTextBox.Text);
            Console.WriteLine("sourceFilePathTextBox.Text:" +sourceFilePathTextBox.Text);
            Console.WriteLine("targetFileNamesTextBox.Text:" +targetFileNamesTextBox.Text);
            Console.WriteLine("开始处理.Text----------------------------------------------");

            if (targetFileNamesTextBox.Text != null && targetFileNamesTextBox.Text.Length > 0)
            {
                if (sourceFilePathTextBox.Text != null && sourceFilePathTextBox.Text.Length > 0)
                {
                    string[] targetFileNames = targetFileNamesTextBox.Text.Split('\n');
                    for (int i = 1; i <= targetFileNames.Length; i++) {
                        string sourceFileName = sourceFileNameTextBox.Text.Replace("{i}",i.ToString());
                        string sourceFileAllPathName = sourceFilePathTextBox.Text + "\\" + sourceFileName;
                        string suffix = Path.GetExtension(sourceFileAllPathName);
                        string targetAllPathName = sourceFilePathTextBox.Text + "\\" + targetFileNames[i-1].Replace("\r\n", "").Replace("\n", "").Replace("\r", "") + suffix;

                        Console.WriteLine("原文件名:"+sourceFileAllPathName);
                        Console.WriteLine("新文件名:" + targetAllPathName);

                        if (File.Exists(sourceFileAllPathName))
                        {

                            Console.WriteLine("开始修改文件名---------------------------------------------");
                            File.Move(sourceFileAllPathName, targetAllPathName);
                            Console.WriteLine("改名成功");
                        }
                    }
                    MessageBox.Show("文件修改完成");
                }
                else {
                    MessageBox.Show("原文件路径不能为空", "错误");
                }
            }
            else {
                MessageBox.Show("新文件名称不能为空", "错误");
            }

        }

        private void clear_Click(object sender, EventArgs e)
        {
            sourceFileNameTextBox.Text = "";
            sourceFilePathTextBox.Text = "";
            targetFileNamesTextBox.Text = "";
            spitTextBox.Text = "";
        }

        private void splitAndSubmit_Click(object sender, EventArgs e)
        {
            string spitStr = spitTextBox.Text;
            string result = "";
            if (targetFileNamesTextBox.Text != null && targetFileNamesTextBox.Text.Length > 0)
            {
                string[] lines = targetFileNamesTextBox.Text.Split('\n');
                foreach (var item in lines)
                {
                    string[] keys = item.Replace("\r\n", "").Replace("\n", "").Replace("\r", "").Split(new string[] { spitStr }, StringSplitOptions.None);
                    foreach (var item1 in keys)
                    {
                        result = result + item1 + "\r\n";
                    }

                }
                targetFileNamesTextBox.Text = result;
            }
            submit_Click(sender, e);
        }

        private void saveConfig_Click(object sender, EventArgs e)
        {
            Dictionary<String, String> keys = new Dictionary<String, String>();

            FileStream fileStream = new FileStream("incrementRename.data", FileMode.Create);
            BinaryFormatter binaryFormatter = new BinaryFormatter();

            keys.Add("sourceFileNameText", this.sourceFileNameTextBox.Text);
            keys.Add("sourceFilePathText", this.sourceFilePathTextBox.Text);
            keys.Add("spitText", this.spitTextBox.Text);

            binaryFormatter.Serialize(fileStream, keys);

            fileStream.Close();
            //CacheHandler.Cache(this.sourceFileNameTextBox.Text, this.sourceFilePathTextBox.Text);
        }

        private void customerStartButton_Click(object sender, EventArgs e)
        {
            Console.WriteLine("starNumberTextBox.Text:" + starNumberTextBox.Text);
            Console.WriteLine("sourceFileNameTextBox.Text:" + sourceFileNameTextBox.Text);
            Console.WriteLine("sourceFilePathTextBox.Text:" + sourceFilePathTextBox.Text);
            Console.WriteLine("targetFileNamesTextBox.Text:" + targetFileNamesTextBox.Text);
            Console.WriteLine("开始处理.Text----------------------------------------------");

            if (!(starNumberTextBox.Text != null && starNumberTextBox.Text.Length > 0)) {
                MessageBox.Show("请输入起始数字", "错误");
            }

            int starNumber = int.Parse(starNumberTextBox.Text);
            if (targetFileNamesTextBox.Text != null && targetFileNamesTextBox.Text.Length > 0)
            {
                if (sourceFilePathTextBox.Text != null && sourceFilePathTextBox.Text.Length > 0)
                {
                    string[] targetFileNames = targetFileNamesTextBox.Text.Split('\n');
                    for (int i = 1; i <= targetFileNames.Length; i++)
                    {
                        string sourceFileName = sourceFileNameTextBox.Text.Replace("{i}", starNumber.ToString());
                        string sourceFileAllPathName = sourceFilePathTextBox.Text + "\\" + sourceFileName;
                        string suffix = Path.GetExtension(sourceFileAllPathName);
                        string targetAllPathName = sourceFilePathTextBox.Text + "\\" + targetFileNames[i - 1].Replace("\r\n", "").Replace("\n", "").Replace("\r", "") + suffix;

                        Console.WriteLine("原文件名:" + sourceFileAllPathName);
                        Console.WriteLine("新文件名:" + targetAllPathName);

                        if (File.Exists(sourceFileAllPathName))
                        {

                            Console.WriteLine("开始修改文件名---------------------------------------------");
                            File.Move(sourceFileAllPathName, targetAllPathName);
                            Console.WriteLine("改名成功");
                        }
                        starNumber = starNumber + 1;
                    }
                    MessageBox.Show("文件修改完成");
                }
                else
                {
                    MessageBox.Show("原文件路径不能为空", "错误");
                }
            }
            else
            {
                MessageBox.Show("新文件名称不能为空", "错误");
            }
        }
    }
}
